package org.xm.sk.controller.dbase;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.xm.sk.exception.DataNotFoundException;
import org.xm.sk.exception.RequestParamterException;
import org.xm.sk.service.data.BaseService;
import org.xm.sk.vo.ResultMsg;
import org.xm.sk.vo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "user" })
@RestController
@RequestMapping(value="/users")
public class UserController {
    
    @Autowired
    private MessageSource messages;
    @Autowired
    private BaseService<User> userService;
    
    @ApiOperation(value="创建用户", notes="根据user对象创建用户，使用此接口需要ADMIN权限")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value="", method=RequestMethod.POST)
    public ResultMsg add(@RequestBody User user) {
        userService.add(user);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiOperation(value="删除用户", notes="根据URI的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public ResultMsg delete(@PathVariable Integer id) {
        userService.delete(id);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }
    
    @ApiOperation(value="更新用户详细信息", notes="根据URI的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "user", value = "待更新的用户详细实体user", required = true, dataType = "User")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public ResultMsg update(@PathVariable Integer id, @RequestBody User user) throws DataNotFoundException {
        User u = userService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(User.class.getName());
        }
        user.setId(id);
        userService.update(user);
        return new ResultMsg("200", messages.getMessage("success", null, "SUCCESS", null), new Date());
    }

    @ApiOperation(value="获取用户详细信息", notes="根据URI的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "int")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User load(@PathVariable Integer id) throws DataNotFoundException {
        User u = userService.findById(id);
        if (null == u) {
            throw new DataNotFoundException(User.class.getName());
        }
        return u;
    }
    
    @ApiOperation(value="获取用户列表", notes="根据提交的分页信息列出所以用户信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "size", value = "每页数量", required = true, paramType = "path", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "int", paramType = "path")
    })
    @RequestMapping(value={"/{size}/{page}"}, method=RequestMethod.GET)
    public MapList<User> list(@PathVariable Integer size, @PathVariable Integer page) throws RequestParamterException {
        if (null == size || null == page || 1 > size || 0 > page) {
            throw new RequestParamterException();
        }
        Page<User> u = (Page<User>) userService.findAllByPage(page, size);
        Long allCount = u.getTotalElements();
        return new MapList<User>(allCount, u.getContent());
    }
}
